package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.linlinjava.litemall.db.dao.LitemallAdMapper;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallAdExample;
import org.linlinjava.litemall.db.vo.LitemallAdAllsVO;
import org.linlinjava.litemall.db.vo.LitemallPosAdAllsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * litemall广告服务
 *
 * @author yliyun
 * @date 2022/09/15
 */
@Service
public class LitemallAdService {

    /**
     * 广告映射器
     */
    @Resource
    private LitemallAdMapper adMapper;

    @Resource
    private LitemallStorageService litemallStorageService;

    /**
     * 查询索引
     *
     * @return {@link List}<{@link LitemallAd}>
     */
    public List<LitemallAd> queryIndex() {
        LitemallAdExample example = new LitemallAdExample();
        example.or().andPositionEqualTo((byte) 1).andDeletedEqualTo(false).andEnabledEqualTo(true);
        return adMapper.selectByExample(example);
    }

    /**
     * 查询选择性
     *
     * @param name    名字
     * @param content 内容
     * @param page    页面
     * @param limit   限制
     * @param sort    排序
     * @param order   订单
     * @return {@link List}<{@link LitemallAd}>
     */
    public IPage<LitemallAd> querySelective(String name, String content, Integer page, Integer limit, String sort, String order) {
        LambdaQueryWrapper<LitemallAd> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name),LitemallAd::getName,name);
        lambdaQueryWrapper.like(!StringUtils.isEmpty(content),LitemallAd::getContent,content);
        lambdaQueryWrapper.eq(LitemallAd::getDeleted,false);
        lambdaQueryWrapper.orderByDesc(LitemallAd::getAddTime);
        Page<LitemallAd> litemallAdPage = adMapper.selectPage(new Page<>(page, limit), lambdaQueryWrapper);
        List<LitemallAd> litemallAds = litemallAdPage.getRecords().stream().map(posAd -> {
                    LitemallAdAllsVO litemallAdAllsVO = new LitemallAdAllsVO();
                    litemallAdAllsVO.setImgObjs(Arrays.asList(posAd.getImgObj()).stream().map(imgObjId -> litemallStorageService.findById(imgObjId)).collect(Collectors.toList()));
                    litemallAdAllsVO.setStaleDated(LocalDateTime.now().isAfter(posAd.getStartTime()) && LocalDateTime.now().isBefore(posAd.getEndTime()));
                    BeanUtils.copyProperties(posAd, litemallAdAllsVO);
                    return litemallAdAllsVO;
                })
                .collect(Collectors.toList());
        litemallAdPage.setRecords(litemallAds);
        return litemallAdPage;
    }

    /**
     * 更新通过id
     *
     * @param ad 广告
     * @return int
     */
    public int updateById(LitemallAd ad) {
        ad.setUpdateTime(LocalDateTime.now());
        return adMapper.updateByPrimaryKeySelective(ad);
    }

    /**
     * 删除通过id
     *
     * @param id id
     */
    public void deleteById(Integer id) {
        adMapper.logicalDeleteByPrimaryKey(id);
    }

    /**
     * 添加
     *
     * @param ad 广告
     */
    public void add(LitemallAd ad) {
        ad.setAddTime(LocalDateTime.now());
        ad.setUpdateTime(LocalDateTime.now());
        adMapper.insertSelective(ad);
    }

    /**
     * 广告详情通过ID
     *
     * @param id id
     * @return {@link LitemallAd}
     */
    public LitemallAd findById(Integer id) {
        return adMapper.selectByPrimaryKey(id);
    }
}
